#include <iostream>
/*
 * @Author: 缄默
 * @Date: 2021-09-16 22:01:05
 * @LastEditors: 缄默
 * @LastEditTime: 2021-12-13 19:08:52
 */

using namespace std;

//懒汉式加载 用到时在加载静态对象
class LazySingleTon {
private:
    static LazySingleTon *_instance;
    LazySingleTon() {
        cout << "LazySingleTon.." << endl;
    }
public:
    static LazySingleTon *getInstance() {
        if(!_instance) {
            _instance = new LazySingleTon();
        }
        return _instance;
    }
};

LazySingleTon *LazySingleTon::_instance = nullptr;

//饿汉式加载 获得前一定已经生成过单例对象
class HungrySingleTon {
private:
    static HungrySingleTon* _instance;
    HungrySingleTon() {
        cout << "HungrySingleTon..." << endl;
    }

public:
    static HungrySingleTon* getInstance() {
        return _instance;
    }
};

HungrySingleTon *HungrySingleTon::_instance = new HungrySingleTon();
int main() {
    LazySingleTon *LazySingleTon1 = LazySingleTon::getInstance();
    cout << LazySingleTon1 << endl;
    LazySingleTon *LazySingleTon2 = LazySingleTon::getInstance();
    cout << LazySingleTon2 << endl;

    HungrySingleTon* HungrySingleTon1 = HungrySingleTon::getInstance();
    HungrySingleTon* HungrySingleTon2 = HungrySingleTon::getInstance();
    cout << HungrySingleTon1 << endl;
    cout << HungrySingleTon2 << endl;
    delete LazySingleTon::getInstance();
    delete HungrySingleTon::getInstance();
    return 0;
}